package com.gym.service.user.impl;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gym.VO.ResultVO;
import com.gym.enums.ResultEnum;
import com.gym.form.user.LoginForm;
import com.gym.pojo.user.UserBaseInfo;
import com.gym.pojo.user.UserDetailInfo;
import com.gym.service.user.LoginService;
import com.gym.service.user.UserService;
import com.gym.utils.ResultVOUtil;
import com.gym.utils.UserValidate;

@Service
// @Slf4j
public class LoginServiceImpl implements LoginService {

	private Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private UserService userService;

	@Override
	public ResultVO login(LoginForm loginForm) {
		try {

			UserBaseInfo userBaseInfo = null;

			/**
			 * 账户名或密码不能为空
			 */
			if (UserValidate.isNull(loginForm.getLoginName())
					|| UserValidate.isNull(loginForm.getUserPassword())) {
				return ResultVOUtil
						.error(ResultEnum.LOGINNAME_OR_PASSWORF_NOT_NULL);
			}

			// 通过账号(包括用户名，手机号，学号)获取用户信息
			userBaseInfo = getUser(loginForm.getLoginName());
			if (userBaseInfo == null) {// 不存在该账号
				return ResultVOUtil
						.error(ResultEnum.LOGINNAME_OR_PASSWORD_ERROR);
			}

			// 用户登录认证
			Subject currentUser = SecurityUtils.getSubject();
			// if (!currentUser.isAuthenticated()) {
			// 把用户名和密码封装为 UsernamePasswordToken 对象
			UsernamePasswordToken token = new UsernamePasswordToken(
					userBaseInfo.getUserName(), loginForm.getUserPassword());

			// remember
			token.setRememberMe(true);
			try {
				currentUser.login(token);
			} /*
			 * catch (UnknownAccountException error) { return ResultVOUtil
			 * .error(ResultEnum.LOGINNAME_OR_PASSWORD_ERROR); }
			 */catch (IncorrectCredentialsException incorrecterror) {
				return ResultVOUtil
						.error(ResultEnum.LOGINNAME_OR_PASSWORD_ERROR);
			} catch (LockedAccountException lockerror) {
				return ResultVOUtil.error(ResultEnum.ACCOUNT_HAS_BEEN_LOCKED);
			}
			return ResultVOUtil.success(userBaseInfo);
		} catch (Exception e) {
			log.error("LoginServiceImpl-login出现了未知异常：" + e.getMessage());
			return ResultVOUtil.error(ResultEnum.UNKNOW_EXCEPTION);
		}
	}

	@Override
	public UserBaseInfo getUser(String accountInfo) {

		UserBaseInfo userBaseInfo = null;

		/**
		 * 判断是否是数字
		 */
		if (UserValidate.isDigit(accountInfo)) {
			UserDetailInfo userDetailInfo = null;
			if (UserValidate.isHandset(accountInfo)) {// 如果是手机号码
				// 通过手机查找用户信息
				userDetailInfo = this.userService
						.getUserDetailInfoByPhoneNumber(accountInfo);
				if (userDetailInfo == null) {
					return null;
				}
			} else { // 否则为学号
				// 按学号查找用户信息
				userDetailInfo = this.userService
						.getUserDetailInfoByStudentId(accountInfo);
				if (userDetailInfo == null) {
					return null;
				}
			}
			userBaseInfo = this.userService.getUserBaseInfoById(userDetailInfo
					.getUserId());
			return userBaseInfo;
		}
		userBaseInfo = this.userService.getUserBaseInfoByName(accountInfo);
		return userBaseInfo;
	}
}
